package com.kejiguan.ui.presenter;

import android.widget.Toast;

import com.android.volley.cache.Cache;
import com.android.volley.error.VolleyError;
import com.android.volley.response.Response;
import com.kejiguan.ui.event.OnOroffEvent;
import com.kejiguan.ui.event.RefreshNeStateEvent;
import com.kejiguan.ui.model.DisplayArea;
import com.kejiguan.ui.model.ExhibitionNeItem;
import com.kejiguan.ui.request.GsonRequest;
import com.kejiguan.ui.request.RequestUtil;
import com.kejiguan.ui.response.GetNeItemInfosResponse;
import com.kejiguan.ui.response.TakeAllOffResponse;
import com.kejiguan.ui.response.TakeAllOnResponse;
import com.kejiguan.ui.util.CacheDataUtil;
import com.kejiguan.ui.util.CommonDate;
import com.kejiguan.ui.util.DateFormatUtil;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by apple on 16/4/5.
 */
public class DisplayFragmentPresenter extends BaseFragmentPresenter {

    /**
     * 单个展项断电
     * */
    public void poweroff(ExhibitionNeItem displayItem) {
        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "701");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("actionFlag", "D");
        params.put("deviceFlag", displayItem.getExhibitionId().substring(0, 1));
        params.put("deviceId", displayItem.getExhibitionId().substring(1));


        final GsonRequest<TakeAllOffResponse> request = new GsonRequest("TakeAllOff", TakeAllOffResponse.class, new Response.Listener<TakeAllOffResponse>() {
            @Override
            public void onResponse(TakeAllOffResponse response) {

                if (response.getRetCode().equals("0000")) {
                    EventBus.getDefault().post(new OnOroffEvent(false));
                    Toast.makeText(mContext, "关闭电源消息发送成功", Toast.LENGTH_SHORT).show();
                }else {
                    EventBus.getDefault().post(response);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }


    public void takeOff(ExhibitionNeItem displayItem) {

        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "701");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("actionFlag", "G");
        params.put("deviceFlag", displayItem.getExhibitionId().substring(0, 1));
        params.put("deviceId", displayItem.getExhibitionId().substring(1));


        final GsonRequest<TakeAllOffResponse> request = new GsonRequest("TakeAllOff", TakeAllOffResponse.class, new Response.Listener<TakeAllOffResponse>() {
            @Override
            public void onResponse(TakeAllOffResponse response) {

                if (response.getRetCode().equals("0000")) {
                    EventBus.getDefault().post(new OnOroffEvent(false));
                    Toast.makeText(mContext, "关机消息发送成功", Toast.LENGTH_SHORT).show();
                }else {
                    EventBus.getDefault().post(response);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }

    public void takeExceptionOff(String exhibitionId) {

        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "701");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("actionFlag", "G");
        params.put("deviceFlag", exhibitionId.substring(0, 1));
        params.put("deviceId", exhibitionId.substring(1));


        final GsonRequest<TakeAllOffResponse> request = new GsonRequest("TakeAllOff", TakeAllOffResponse.class, new Response.Listener<TakeAllOffResponse>() {
            @Override
            public void onResponse(TakeAllOffResponse response) {

                if (response.getRetCode().equals("0000")) {
                    EventBus.getDefault().post(new OnOroffEvent(false));
                    Toast.makeText(mContext, "展区关机消息发送成功", Toast.LENGTH_SHORT).show();
                } else {
                    EventBus.getDefault().post(response);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }

    public void takeOn(ExhibitionNeItem displayItem) {
        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "701");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("actionFlag", "K");
        params.put("deviceFlag", displayItem.getExhibitionId().substring(0, 1));
        params.put("deviceId", displayItem.getExhibitionId().substring(1));


        final GsonRequest<TakeAllOnResponse> request = new GsonRequest("TakeAllOn", TakeAllOnResponse.class, new Response.Listener<TakeAllOnResponse>() {
            @Override
            public void onResponse(TakeAllOnResponse response) {

                if (response.getRetCode().equals("0000")) {
                    EventBus.getDefault().post(new OnOroffEvent(true));
                    Toast.makeText(mContext, "开机消息发送成功", Toast.LENGTH_SHORT).show();
                } else {
                    EventBus.getDefault().post(response);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }

    public void takeExhibitionOn(String exhibitionId) {
        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "701");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("actionFlag", "K");
        params.put("deviceFlag", exhibitionId.substring(0, 1));
        params.put("deviceId", exhibitionId.substring(1));


        final GsonRequest<TakeAllOnResponse> request = new GsonRequest("TakeAllOn", TakeAllOnResponse.class, new Response.Listener<TakeAllOnResponse>() {
            @Override
            public void onResponse(TakeAllOnResponse response) {

                if (response.getRetCode().equals("0000")) {
                    EventBus.getDefault().post(new OnOroffEvent(true));
                    Toast.makeText(mContext, "展区开机消息发送成功", Toast.LENGTH_SHORT).show();
                }else {
                    EventBus.getDefault().post(response);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }


    public void takeDCOff() {

        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "701");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("actionFlag", "G");
        params.put("deviceFlag", "Z");
        params.put("deviceId", "DC");


        final GsonRequest<TakeAllOffResponse> request = new GsonRequest("TakeAllOff", TakeAllOffResponse.class, new Response.Listener<TakeAllOffResponse>() {
            @Override
            public void onResponse(TakeAllOffResponse response) {

                if (response.getRetCode().equals("0000")) {
                    EventBus.getDefault().post(new OnOroffEvent(false));
                    Toast.makeText(mContext, "神舟与天宫关机消息发送成功", Toast.LENGTH_SHORT).show();
                } else {
                    EventBus.getDefault().post(response);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }


    public void takeDCOn() {

        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "701");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);
        params.put("userName", CacheDataUtil.getInstance().getmUser().getUserName());
        params.put("actionFlag", "K");
        params.put("deviceFlag", "Z");
        params.put("deviceId", "DC");


        final GsonRequest<TakeAllOffResponse> request = new GsonRequest("TakeAllOff", TakeAllOffResponse.class, new Response.Listener<TakeAllOffResponse>() {
            @Override
            public void onResponse(TakeAllOffResponse response) {

                if (response.getRetCode().equals("0000")) {
                    EventBus.getDefault().post(new OnOroffEvent(false));
                    Toast.makeText(mContext, "神舟与天宫开机消息发送成功", Toast.LENGTH_SHORT).show();
                } else {
                    EventBus.getDefault().post(response);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }


    public void getNeItemInfos() {

        long currentTime = System.currentTimeMillis();
        String currentDate = String.valueOf(currentTime);
        try {
            currentDate = DateFormatUtil.longToString(currentTime, "yyyyMMdd");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Map<String, String> params = new HashMap<>();
        params.put("requestCode", "703");
        params.put("messageId", currentDate + "S" + currentTime);
        params.put("systemCode", CommonDate.SystemCode);

        final GsonRequest<GetNeItemInfosResponse> request = new GsonRequest("getNeItemInfos", GetNeItemInfosResponse.class, new Response.Listener<GetNeItemInfosResponse>() {
            @Override
            public void onResponse(GetNeItemInfosResponse response) {

                if ("0000".equals(response.getRetCode())&&null!=response.getExhibitionRecordMap()) {

                    Set<String> keys=response.getExhibitionRecordMap().keySet();
                    Iterator<String> stringIterator=keys.iterator();
                    while (stringIterator.hasNext())
                    {
                        String key=stringIterator.next();
                        String[] mutyKeys=key.split("=");

                        if (CacheDataUtil.getInstance().getDisPlayAreaIdNames().contains(key))
                        {

                            int disPalyAreaCount=CacheDataUtil.getInstance().getDisplayAreas().size();
                            for (int i=0;i<disPalyAreaCount;i++)
                            {
                                if (mutyKeys[0].equals(CacheDataUtil.getInstance().getDisplayAreas().get(i).getDisplayId()))
                                {
                                    Collections.sort(response.getExhibitionRecordMap().get(key));
                                    CacheDataUtil.getInstance().getDisplayAreas().get(i).setExhibitionNeItems(response.getExhibitionRecordMap().get(key));
                                }
                            }

                        }else//新增操作
                        {

                            DisplayArea displayArea=new DisplayArea();
                            displayArea.setDisplayId(mutyKeys[0]);
                            displayArea.setDisplayName(mutyKeys[1]);
                            Collections.sort(response.getExhibitionRecordMap().get(key));
                            displayArea.setExhibitionNeItems(response.getExhibitionRecordMap().get(key));

                            CacheDataUtil.getInstance().getDisplayAreas().add(displayArea);
                            CacheDataUtil.getInstance().getDisPlayAreaIdNames().add(key);
                        }
                    }

                    for (int i=0;i<CacheDataUtil.getInstance().getDisplayAreas().size();i++)
                    {
                        DisplayArea displayArea= CacheDataUtil.getInstance().getDisplayAreas().get(i);
                        displayArea.initDatas();
                    }
                }

                EventBus.getDefault().post(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(mContext, "获取网元信息失败", Toast.LENGTH_SHORT).show();
            }
        });

        RequestUtil.sendRequest(mContext, params, request);
    }

}
